<template>
    <div class="Register" v-loading="loading">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item prop="name">
                <el-input placeholder="请输入用户名(昵称)" v-model.trim="ruleForm.name" autocomplete="off">
                    <i slot="prefix" class="el-input__icon el-icon-user"></i>
                </el-input>
            </el-form-item>
            <el-form-item prop="email">
                <el-input placeholder="请输入账号(邮箱号)" v-model="ruleForm.email" autocomplete="off">
                    <i slot="prefix" class="el-input__icon el-icon-message"></i>
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" placeholder="请输入密码" show-password v-model.trim="ruleForm.password" autocomplete="off">
                    <i slot="prefix" class="el-input__icon el-icon-lock"></i>
                </el-input>
            </el-form-item>
            <el-form-item prop="CheckPwd">
                <el-input type="password" placeholder="请确认密码" show-password v-model.trim="ruleForm.CheckPwd" autocomplete="off">
                    <i slot="prefix" class="el-input__icon el-icon-lock"></i>
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" style="width: 100%;" @click="submitForm('ruleForm')">注册</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import loginApi from '../../api/apiUser';
export default {
    name: "Register",
    data() {
        var checkname = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('用户名不能为空'));
            }else{
                callback()
            }
        };
        var checkemail = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('账号不能为空'));
            }
            let reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
            if(!reg.test(value)){
                return callback(new Error('账号格式有误！'));
            }else{
                callback()
            }
        };
        var checkPwd = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('密码不能为空'));
            }
            let reg = /^\w{5,7}$/
            if(!reg.test(value)){
                return callback(new Error('密码格式有误，请输入6-8位数字字母组合！'));
            }else{
                callback()
            }
        };
        var checkPassPwd= (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                name:"",
                email:"",
                password:"",
                CheckPwd:""
            },
            rules: {
                name:[
                    { validator: checkname, trigger:  ['blur', 'change'] }
                ],
                email: [
                    { validator: checkemail, trigger:  ['blur', 'change'] }
                ],
                password: [
                    { validator: checkPwd, trigger:  ['blur', 'change'] }
                ],
                CheckPwd:[
                { validator: checkPassPwd, trigger:  ['blur', 'change'] }
                ]
            },
            loading:false
        }
    },
    methods: {
        submitForm(name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.register()
                } else {
                    return false;
                }
            });
        },
        async register(){
          this.loading = true
          const result =   await  loginApi.register(this.ruleForm)
          this.loading = false 
          if(result.code != 200) return this.$message.error('注册失败')
          this.$message.success('注册成功')
          const loginreult = await loginApi.loging(this.ruleForm)
          localStorage.setItem("token",loginreult.data)
          this.$router.push('/')
        }
    }
}
</script>
<style lang="less" scoped>
.Register {
    width: 90%;
    height: 100%;
    margin: 0 auto;

    .demo-ruleForm {
        padding-top: 30px;
    }
}

/deep/ .el-form-item__content {
    margin-left: 0 !important;
}
</style>